package utils

import (
	"net/http"

	"github.com/GoAdminGroup/go-admin/modules/logger"
	"github.com/gin-gonic/gin"

	"app/api/schema"
	rich_err "app/error"
)

func GinJSONWithRichErr(c *gin.Context, body interface{}, richErr *rich_err.RichError) {
	code := http.StatusOK
	var err error
	if richErr != nil {
		code, err = richErr.HTTP()
		url := c.Request.URL.String()
		method := c.Request.Method
		bodyBytes, _ := c.GetRawData()
		// 将请求体字节转换为字符串
		bodyStr := string(bodyBytes)
		logger.Errorf("url: %s, method: %s, body: %s, have error: %s", url, method, bodyStr, err.Error())
	}
	c.JSON(code, schema.CreateResp(code, body, err))
}

func GinJSONPageWithRichErr(c *gin.Context, body, pager interface{}, richErr *rich_err.RichError) {
	code := http.StatusOK
	var err error
	if richErr != nil {
		code, err = richErr.HTTP()
		url := c.Request.URL.String()
		method := c.Request.Method
		bodyBytes, _ := c.GetRawData()
		// 将请求体字节转换为字符串
		bodyStr := string(bodyBytes)
		logger.Errorf("url: %s, method: %s, body: %s, have error: %s", url, method, bodyStr, err.Error())
	}
	c.JSON(code, schema.CreatePageResp(code, body, pager, err))
}
